package com.test.nnooolwdeaer.view_wooden.widget_wooden;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.test.nnooolwdeaer.R;
import com.test.nnooolwdeaer.dialog_wooden.AddressSelectDialog_Niri_wooden;
import com.test.nnooolwdeaer.dialog_wooden.AddressSelectDialog_Viet_wooden;
import com.test.nnooolwdeaer.util_wooden.StringUtil_wooden;


public class AddressPickView_Viet_wooden extends LinearLayout {

    private String title_wooden,hint_wooden;
    private boolean isMustInput_wooden = true;

    private TextView titleTv_wooden;
    private TextView inputTv_wooden;
    private View clickView_wooden;

    private AddressSelectDialog_Niri_wooden addressSelectDialog_wooden;

    private String state_wooden, city_wooden;

    public AddressPickView_Viet_wooden(Context context) {
        super(context);
        init_wooden(context,null);
    }

    public AddressPickView_Viet_wooden(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init_wooden(context,attrs);
    }

    public AddressPickView_Viet_wooden(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init_wooden(context,attrs);
    }


    protected void init_wooden(Context context,AttributeSet attrs){

        if (attrs!=null){
            @SuppressLint("Recycle") TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.AddressPickView_Viet_wooden);
            title_wooden = typedArray.getString(R.styleable.AddressPickView_Viet_wooden_title);
            hint_wooden = typedArray.getString(R.styleable.AddressPickView_Viet_wooden_android_hint);
        }

        View view = View.inflate(context, R.layout.view_address_pick_wooden,this);
        titleTv_wooden = view.findViewById(R.id.view_address_pick_tv_title);
        inputTv_wooden = view.findViewById(R.id.view_address_pick_tv_input);
        clickView_wooden = view.findViewById(R.id.view_address_pick_layout_click);

        setTitle(title_wooden);
        inputTv_wooden.setHint(StringUtil_wooden.getSafeString_wooden(hint_wooden));

        clickView_wooden.setOnClickListener(v -> new AddressSelectDialog_Viet_wooden(context, inputTv_wooden, addressStrs -> {
            if (addressStrs.length<2) return;
            state_wooden = addressStrs[0];
            city_wooden = addressStrs[1];
        }).show());

    }

    public boolean isInputEmpty() {
        return TextUtils.isEmpty(inputTv_wooden.getText());
    }

    public String getInput_wooden() {
        return StringUtil_wooden.getSafeString_wooden(inputTv_wooden.getText().toString());
    }

    public void setInput(String text){
        inputTv_wooden.setText(StringUtil_wooden.getSafeString_wooden(text));
    }

    public void setTitle(String title){
        if (TextUtils.isEmpty(title)) titleTv_wooden.setVisibility(GONE);
        else {
            titleTv_wooden.setVisibility(VISIBLE);
            titleTv_wooden.setText(title);
        }
    }

    public String getState_wooden() {
        return StringUtil_wooden.getSafeString_wooden(state_wooden);
    }

    public String getCity_wooden() {
        return StringUtil_wooden.getSafeString_wooden(city_wooden);
    }

    public void setMustInput_wooden(boolean mustInput_wooden) {
        isMustInput_wooden = mustInput_wooden;
    }
}
